package com.celesea.auth.core.util;

import java.io.Serializable;

/**
 * 此类描述的是：
 * 
 * @Title: Page.java
 * @Package cn.ssh.core.dao
 * @author: 317075064@qq.com
 * @date: 2013-6-20 下午2:50:14
 * @version: 1.0
 */

public class Page implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8968914019815055930L;
	/**
	 * 
	 */
	private int current;
	/**
	 * 
	 */
	private int pageSize;
	/**
	 * 用于确定是否需要统计数量
	 */
	private boolean getTotalCount=true;

	/**
	 * 
	 * @param page
	 * @param pageSize
	 */
	public Page(int page, int pageSize) {
		this(page, pageSize, true);
	}

	/**
	 * 
	 * @param page
	 * @param pageSize
	 * @param getTotalCount
	 */
	public Page(int page, int pageSize, boolean getTotalCount) {
		super();
		this.current = page;
		this.pageSize = pageSize;
		this.getTotalCount = getTotalCount;
	}

	/**
	 * 
	 * @return
	 */
	public static Page limitOne() {
		return new Page(1, 1, false);
	}

	/**
	 * 
	 * @param dataBuffer
	 * @return
	 */
	public StringBuffer toPageString(StringBuffer dataBuffer) {
		StringBuffer buffer = new StringBuffer(80);
		buffer.append("select * from(select t.*,rownum rn from(");
		buffer.append(dataBuffer);
		buffer.append(") t where rownum<=" + current * pageSize + ") where rn>" + (current - 1) * pageSize + " ");
		return buffer;
	}

	/**
	 * 
	 * @return
	 */
	public int getStartRecord() {
		return (current - 1) * pageSize;
	}

	/**
	 * current
	 * 
	 * @return the current
	 * @since CodingExample Ver(编码范例查看) 1.0
	 */

	public int getCurrent() {
		return current;
	}

	/**
	 * @param current
	 *            the current to set
	 */

	public void setCurrent(int current) {
		this.current = current;
	}

	/**
	 * pageSize
	 * 
	 * @return the pageSize
	 * @since CodingExample Ver(编码范例查看) 1.0
	 */

	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 此方法描述的是：
	 * 
	 * @Title: Page.java
	 * @Package cn.ssh.core.dao
	 * @author: 317075064@qq.com
	 * @date: 2013-8-9 下午3:52:26
	 * @version: 1.0
	 */

	public boolean isCountPage() {
		return pageSize > 0 && getTotalCount;
	}

	/**
	 * 此方法描述的是：
	 * 
	 * @Title: Page.java
	 * @Package cn.ssh.core.dao
	 * @author: 317075064@qq.com
	 * @date: 2013年9月10日 上午9:32:04
	 * @version: 1.0
	 */

	public boolean isLimitQuery() {
		return current > 0 && pageSize >= 1;
	}

    public Pager toPager(long total) {
		Pager pager=new Pager();
		pager.setCurrent(current);
		pager.setPageSize(pageSize);
		pager.setTotal(total);
		return pager;
    }
}
